package com.eco.fanliapp.job.goodsfree;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eco.fanliapp.dao.AppGoodsMapper;
import com.eco.fanliapp.dao.GoodsFreeIntegralMapper;
import com.eco.fanliapp.dao.SyChannelGoodsMapper;
import com.eco.fanliapp.dao.SyTljGoodsfreeMapper;
import com.eco.fanliapp.entity.*;
import com.eco.fanliapp.service.FindItemIdUtils;
import com.eco.fanliapp.service.IAppGoodsService;
import com.eco.fanliapp.service.IUserCashschemeService;
import com.eco.fanliapp.utils.Convert;
import com.eco.fanliapp.utils.DateUtils;
import com.eco.fanliapp.vo.HaoDanKuItemInfo;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 积分兑换过期商品定时扫描 一分钟一次
 */
@DisallowConcurrentExecution
@PersistJobDataAfterExecution
public class GoodsFreeIntegralDownTask implements Job, Serializable {


    private static final Logger log = LoggerFactory.getLogger(GoodsFreeIntegralDownTask.class);

    @Autowired
    private GoodsFreeIntegralMapper goodsFreeIntegralMapper;
    @Autowired
    private AppGoodsMapper appGoodsMapper;
    @Autowired
    private IAppGoodsService appGoodsService;
    @Autowired
    private FindItemIdUtils taoBaoUtils;
    @Autowired
    private IUserCashschemeService userCashschemeService;
    @Autowired
    private SyChannelGoodsMapper syChannelGoodsMapper;
    @Autowired
    private SyTljGoodsfreeMapper syTljGoodsfreeMapper;

    public GoodsFreeIntegralDownTask() {
    }

    @Override
    public void execute(JobExecutionContext context) {

        //取得job详情
        JobDetail jobDetail = context.getJobDetail();

        JobDataMap dataMap = jobDetail.getJobDataMap();

        int executeCount = 0;
        if (dataMap.containsKey("runCount")) {
            executeCount = dataMap.getInt("runCount");
        }

        // 增量计数并将其存储回 JobDataMap，这样可以适当保持工作状态
        executeCount++;
        dataMap.put("runCount", executeCount);

        long startTime = System.currentTimeMillis();
        log.info("***************************************开始执行积分兑换过期商品定时扫描任务-GoodsFreeIntegralDownTask：executeCount=" + executeCount + "--startTime=" + startTime);

        //当前时间
        long startMillions = System.currentTimeMillis();

        try {
            QueryWrapper<GoodsFreeIntegral> QueryWrapper = new QueryWrapper();
            List<GoodsFreeIntegral> goodsFreeIntegralList = goodsFreeIntegralMapper.selectList(QueryWrapper);
            AppCompany com = userCashschemeService.findCompanyAll();
            String itemids = "";
            for (GoodsFreeIntegral dataBean : goodsFreeIntegralList) {
                AppGoods appGoods = new AppGoods();
                appGoods.setItemid(dataBean.getGoodsId());
                Double itemendprice = null;

                String itemprice = null;
                String tkmoney = null;
                Double couponmoney = null;
                HaoDanKuItemInfo info = taoBaoUtils.getHaoDanKuItemId(appGoods.getItemid());
                if (info != null && info.getCode() == 1 && info.getData() != null) {
                    itemendprice = info.getData().getItemendprice() == null ? 0d : Double.valueOf(info.getData().getItemendprice());
                    tkmoney = info.getData().getTkmoney();
                    itemprice = info.getData().getItemprice();
                    couponmoney = info.getData().getCouponmoney() == null ? 0d : Double.valueOf(info.getData().getCouponmoney());
                } else {
                    AppGoods appGoods_new = appGoodsService.getMoneyInfoByGoodsId(com, appGoods);
                    if (appGoods_new != null) {
                        itemendprice = appGoods_new.getItemendprice() == null ? 0 : appGoods_new.getItemendprice();
                        tkmoney = appGoods_new.getTkmoney();
                        itemprice = appGoods_new.getItemprice();
                        couponmoney = appGoods_new.getCouponmoney();
                    }
                }
                //删除失效商品
                if (!dataBean.getFixedAmount().equals(itemendprice) || itemendprice >= 7 || dataBean.getEndTime() <= System.currentTimeMillis()) {
                    itemids += dataBean.getGoodsId() + ",";
                    //判断商品是否存在淘礼金,高佣中
                    SyChannelGoods syChannelGoods = syChannelGoodsMapper.selectChannelGoodsById(Long.valueOf(dataBean.getGoodsId()));
                    SyTljGoodsfree syTljGoodsfree = syTljGoodsfreeMapper.selectTljGoodsfreeByItemId(Long.valueOf(dataBean.getGoodsId()));
                    if (syTljGoodsfree != null && DateUtils.date2Strday(syTljGoodsfree.getUpdateTime()).equals(DateUtils.date2Strday(new Date())) || syChannelGoods != null) {
                    } else {
                        int i = appGoodsMapper.updateFreeGoods(dataBean.getGoodsId(), itemendprice, tkmoney, itemprice, couponmoney);
                        if (i > 0) {
                            log.info("更新商品表商品成功");
                        }
                    }
                }
            }
            if (!"".equals(itemids)) {
                int i = goodsFreeIntegralMapper.deleteByGoodsIds(Convert.toStrArray(itemids));
                if (i > 0) {
                    log.info("积分兑换商品删除成功   dataBean=" + JSON.toJSONString(itemids));
                } else {
                    log.info("积分兑换商品删除失败");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("执行积分兑换商品定时扫描任务错误");
        }

        long endTime = System.currentTimeMillis();
        log.info("***************************************结束执行积分兑换商品定时扫描任务-GoodsFreeIntegralDownTask：executeCount=" + executeCount + "--endTime=" + endTime + "--耗时=" + (endTime - startTime));

    }

}
